#include "constants.h"
#include "car.h"
#include "../arithmetic/fixed_point.h"



s_car   *init(void)
{
  s_car *c = NULL;

  c = malloc(sizeof (s_car));
  c->v = int_to_fp(0);
  c->x = int_to_fp(0);
  c->y = int_to_fp(0);
  c->d = int_to_fp(0);
  c->crash = int_to_fp(0);
  c->cycle = int_to_fp(0);
  return (c);
}

void acceleration(s_car **c)
{
  (*c)->v += FP_ACCELERATION;
}


void braking(s_car **c)
{
  (*c)->v -= FP_BRAKE;
  if ((*c)->v == 0)
    (*c)->v = int_to_fp(0);
}

void    deceleration(s_car **c)
{
  int   tire_f = 0;
  int   air_f = 0;

  if ((*c)->v != 0)
  {
    tire_f = fp_mul((*c)->v, FP_FRICTION_1);
    air_f = fp_mul(fp_mul((*c)->v, (*c)->v), FP_FRICTION_2);

    (*c)->v -= (FP_FRICTION_0 + tire_f + air_f);
    if ((*c)->v == 0)
      (*c)->v = int_to_fp(0);
  }
}
